package rexsee.core.lang;

import java.lang.reflect.Field;
import java.util.Locale;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.utilities.Json;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class RexseeLanguageInterface implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Language";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeLanguageInterface(childBrowser);
	}

	public static final String PREFERENCE_NAME = "language";
	private void setPreference(String name, String value) {
		SharedPreferences preference = mContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString(name, value);
		editor.commit();
	}

	private final Browser mBrowser;
	private final Context mContext;

	public RexseeLanguageInterface(Browser browser) {
		mBrowser = browser;
		mContext = browser.getContext();
	}

	public static String[] toArray(String value) {
		String[] arr = value.split("\\|");
		return arr;
	}
	public static String[][] toArray2(String value) {
		String[] arr = value.split("\\|\\|");
		String[][] arr2 = new String[arr.length][];
		for (int i = 0; i < arr.length; i++) {
			arr2[i] = arr[i].split("\\|");
		}
		return arr2;
	}
	public static void init(Context context) {
		Class<?> c = null;
		Locale locale = context.getResources().getConfiguration().locale;
		String lang = locale.getLanguage().toLowerCase();
		if (lang.equals("zh")) {
			c = (locale.getCountry().toLowerCase().equals("tw")) ? RexseeLanguage_TW.class : RexseeLanguage_CN.class;
			/*
			} else if (lang.equals("de")) {
				c = RexseeLanguage_DE.class;
			} else if (lang.equals("fr")) {
				c = RexseeLanguage_FR.class;
			} else if (lang.equals("ja")) {
				c = RexseeLanguage_JA.class;
			*/
		} else {
			c = RexseeLanguage_EN.class;
		}
		Field[] fields = RexseeLanguage.class.getFields();
		SharedPreferences preference = context.getSharedPreferences(RexseeLanguageInterface.PREFERENCE_NAME, Context.MODE_PRIVATE);
		for (int i = 0; i < fields.length; i++) {
			try {
				String name = fields[i].getName();
				String type = fields[i].getType().getSimpleName();
				String value = preference.getString(name, null);
				if (value != null) {
					if (type.equalsIgnoreCase("String[]")) {
						fields[i].set(null, RexseeLanguageInterface.toArray(value));
					} else if (type.equalsIgnoreCase("String[][]")) {
						fields[i].set(null, RexseeLanguageInterface.toArray2(value));
					} else {
						fields[i].set(null, value);
					}
				} else {
					fields[i].set(null, c.getField(name).get(null));
				}
			} catch (Exception e) {
			}
		}
	}

	//JavaScript interface
	public int size() {
		Field[] fields = RexseeLanguage.class.getFields();
		return fields.length;
	}
	public String get() {
		Field[] fields = RexseeLanguage.class.getFields();
		String rtn = "";
		for (int i = 0; i < fields.length; i++) {
			if (i > 0) rtn += ",";
			rtn += "\"" + fields[i].getName() + "\"";
		}
		return "[" + rtn + "]";
	}
	public String get(String name) {
		try {
			Field field = RexseeLanguage.class.getField(name);
			String type = field.getType().getSimpleName();
			if (type.equalsIgnoreCase("String[]")) {
				String[] arr = (String[]) RexseeLanguage.class.getField(name).get(null);
				return Json.toJson(arr);
			} else if (type.equalsIgnoreCase("String[][]")) {
				String[][] arr = (String[][]) RexseeLanguage.class.getField(name).get(null);
				String rtn = "";
				for (int i = 0; i < arr.length; i++) {
					if (i > 0) rtn += ",";
					rtn += Json.toJson(arr[i]);
				}
				return "[" + rtn + "]";
			} else {
				return String.valueOf(field.get(null));
			}
		} catch (Exception e) {
			return "";
		}
	}
	public boolean set(String name, String value) {
		try {
			Field field = RexseeLanguage.class.getField(name);
			String type = field.getType().getSimpleName();
			if (type.equalsIgnoreCase("String[]")) {
				field.set(null, toArray(value));
			} else if (type.equalsIgnoreCase("String[][]")) {
				field.set(null, toArray2(value));
			} else {
				field.set(null, value);
			}
		} catch (Exception e) {
			return false;
		}
		setPreference(name, value);
		return true;
	}
	public String getType(String name) {
		try {
			return RexseeLanguage.class.getField(name).getType().getSimpleName();
		} catch (Exception e) {
			return "";
		}
	}
	public String getPreference(String name) {
		SharedPreferences preference = mContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		return (preference == null) ? "" : preference.getString(name, "");
	}
	public void clearPreference() {
		SharedPreferences preference = mContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.clear();
		editor.commit();
	}
	public void refresh() {
		init(mContext);
	}

}
